package universum.bi;

import java.util.Comparator;

/**
 * Location defines the point in the space of game universe.
 * Usually user is not intended to create new locations with constructor
 * but get existing location using methods of BeingInterface.
 * In rare occasions where you do need to create new location
 * and absolutely sure about topology you're running on,
 * use BeingInterface.createLocation()
 *
 * @author nike
 * @see BeingInterface
 */
public final class Location {
    private final int x, y;
    private static int _freeID = 0; //Integer.MIN_VALUE; // by cpp
    protected final int _id = _freeID++;
    //protected final int _id;

    /**
     * Create new location in 2D discreet topology.
     * Don't use this constuctor unless you have to, as
     * it will eventually be removed.
     * Use BeingInterface.createLocation(x,y).
     *
     * @see BeingInterface#createLocation(Object...)
     */
    @Deprecated
    public Location(int x, int y) {
        //Constants.checkPerms();
        this.x = x;
        this.y = y;   
        //_id = id;
    }

    public int getID() {
        return _id;
    }


    /**
     * Return x coordinate of this location, actual semantic of this
     * function depends on topology and may be something not so intuitive.
     */
    public int getX() {
        return x;
    }

    /**
     * Return y coordinate of this location, , actual semantic of this
     * function depends on topology and may be something not so intuitive.
     */
    public int getY() {
        return y;
    }

    /*public boolean equals(Object other) {
        if (other == null || !(other instanceof Location)) {
            return false;
        }
        // [BEGIN] modified by cpp
        //Location loc = (Location)other;
        //return loc.getX() == x && loc.getY() == y;
        return _id == ((Location) other)._id;
        // [END]
    }

    public int hashCode() {
        // [BEGIN] modified by cpp
        //return x << 16 | y;
        return _id;
        // [END]
    }*/

    public String toString() {
        return "[" + x + "," + y + "]";
    }

    public static Comparator LocationComparator = new Comparator() {
        public int compare(Object o1, Object o2) {
            /*if (o1 instanceof Location && o2 instanceof Location) */return compare((Location) o1, (Location) o2);
        }

        public int compare(Location one, Location another) {
            int x = one.getID(), y = another.getID();
            if (x < y) return -1;
            if (x > y) return 1;
            return 0;
        }
    };

}
